import { vnode } from "./vmode";
/**
 * 三个参数
 * @param {*} sel 
 * @param {*} data 
 * @param {*} c 
 * @description var vnode = h('ul', {}, [
    h('li', {}, 'tom'),
    h('li', {}, [
        h('div', {}, '123'),
        h('div', {}, '456'),
    ]),
    h('li',{}, 'bryan'),
]);

 */
export function h(sel, data, c) {
    if (arguments.length != 3) {
        throw new Error("函数必须为三个参数，版本为低配h函数")
    }

    //检查c参数
    if (typeof c == 'string' || typeof c == 'number') {
        return vnode(sel, data, undefined, c);
    } else if (Array.isArray(c)) {
        let children = [];
        c.forEach(value => {
            if (!(typeof value == 'object' && value.hasOwnProperty('sel'))) {
                throw new Error("参数没有h函数")
            }
            children.push(value);
        });
        //收集完毕，返回虚拟node
        return vnode(sel, data, children, undefined);
    } else if (typeof c == 'object' && c.hasOwnProperty('sel')) {
        //收集完毕，返回虚拟node
        return vnode(sel, data, [c], undefined);
    } else {
        throw new Error("c参数类型错误")
    }
}